<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

class Backup{
    private $db;
    private $host;
    private $user;
    private $pass;
    private $dbName;
    private $tables;
    
    function __construct()
    {
        $this->db = new DB();
        $this->host = $this->db->get('configManager')->host;
        $this->user = $this->db->get('configManager')->username;
        $this->pass = $this->db->get('configManager')->pwd;
        $this->dbName = $this->db->get('configManager')->dbName;
        $this->tables = '*';
    }
    public function backup_tables()
    {
        $checkDate = date("Ymd");
        $checkFile = dirname($_SERVER['SCRIPT_FILENAME'])."/backup/backup" .$checkDate .".sql";
        
        if(!file_exists($checkFile)){
          $handle = fopen("backup/backup".$checkDate.".sql","w+");
          
          if(!$handle){
                echo "Fail to open the" ."backup".$checkDate.".sql";
          }
            
          $this->db->startConnection();  
    
          //get all of the tables
          if($this->tables == '*')
          {
            $this->tables = array();
            $result = mysql_query('SHOW TABLES');
            while($row = mysql_fetch_row($result))
            {
              $this->tables[] = $row[0];
            }
          }
          else
          {
            $this->tables = is_array($this->tables) ? $this->tables : explode(',',$this->tables);
          }
          $return = '/*!40101 SET NAMES utf8 */;
        
        /*!40101 SET SQL_MODE=""*/;
        
        /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
        /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
        /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE="NO_AUTO_VALUE_ON_ZERO" */;
        /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
        
        CREATE DATABASE /*!32312 IF NOT EXISTS*/`book store` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */;
        
        USE `book store`;';
        
        $return .= "\n\n";
          //cycle through
          foreach($this->tables as $table)
          {
            $result = mysql_query('SELECT * FROM '.$table);
            $num_fields = mysql_num_fields($result);
            
            $return.= 'DROP TABLE IF EXISTS `'.$table.'` ;';
            $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
            $return.= "\n\n".$row2[1].";\n\n";
            
            $return.= 'INSERT INTO `'.$table.'` VALUES';
            for ($i = 0; $i < $num_fields; $i++) 
            {
              while($row = mysql_fetch_row($result))
              {
                $return .= "(";
                for($j=0; $j<$num_fields; $j++) 
                {
                  $row[$j] = addslashes($row[$j]);
                  $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                  if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                  if ($j<($num_fields-1)) { $return.= ','; }
                }
                $return.= "),";
              }
            }
            $return = substr($return, 0, -1);
            $return.=";\n\n\n";
          }
          
          //save file
          $ccyymmdd = date("Ymd");
          $handle = fopen("backup/backup".$ccyymmdd.".sql","w+");
          fwrite($handle,$return);
          $this->db->closeConnection();
          echo "Database backs up successfully!";      
        }
      
    }
    
}
?>